<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

if(isset($_GET["keyword"])) {
  if(!Check::isName($_GET["keyword"])) {
    exit(Result::jsonReturn(1, "参数有误"));
  }
  $db = new Db();
  $keyword = $_GET["keyword"];
  $sql = "SELECT id,title,slug,type,created_time,updated_time FROM qilian_contents WHERE title LIKE '%{$keyword}%' AND type=0";
  $res = $db->getAll($sql);
  foreach($res as $index=>$val) {
    $id = (int)$val["id"];
    $sql = "SELECT title FROM qilian_attachments WHERE content_id={$id}";
    $res_attachments = $db->getAll($sql);
    $res[$index]["attachments"] = array();
    foreach($res_attachments as $val_in) {
      array_push($res[$index]["attachments"], CDN.$val_in["title"]);
    }
  }
  exit(Result::jsonReturn(0, "查询成功", $res));
}else {
    exit(Result::jsonReturn(1, "缺少参数"));
}
?>